package com.example.movie.data.movie.model

import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.forward.androids.utils.ThreadUtil
import com.example.movie.R
import com.example.movie.common.constants.Constants
import com.example.movie.common.constants.LoadingState
import com.example.movie.common.model.RequestResult
import com.example.movie.common.model.Result
import com.example.movie.data.movie.model.vo.MovieDetailVo

class MovieDetailViewModel(private val repository: MovieRepository) : ViewModel() {

    private val _approveState = MutableLiveData<RequestResult<Boolean>>()
    val approveState: MutableLiveData<RequestResult<Boolean>>
        get() = _approveState

    private val _haveSeenState = MutableLiveData<RequestResult<Boolean>>()
    val haveSeenState: MutableLiveData<RequestResult<Boolean>>
        get() = _haveSeenState

    private val _movieDetailData = MutableLiveData<RequestResult<MovieDetailVo>>()
    val movieDetailData: MutableLiveData<RequestResult<MovieDetailVo>>
        get() = _movieDetailData

    fun loadMovieDetailData(movieId: Int) {
        var result: Result<MovieDetailVo>? = null;
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    if (result is Result.Success) {
                        _movieDetailData.value = RequestResult(
                            success = (result as Result.Success<MovieDetailVo>).data
                        )
                    } else {
                        _movieDetailData.value = RequestResult(
                            error = R.string.request_fail
                        )
                    }
                }
            }
        }

        ThreadUtil.getInstance().execute {
            result = repository.getMovieDetailData(movieId)
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }
    }

    fun changeState(movieId: Int, type: Int) {
        var result: Result<Boolean>? = null;
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    if (result is Result.Success) {
                        if (type == Constants.UserMovieTab.HOPE_LOOK) {
                            _approveState.value = RequestResult(
                                success = (result as Result.Success<Boolean>).data
                            )
                        } else {
                            _haveSeenState.value = RequestResult(
                                success = (result as Result.Success<Boolean>).data
                            )
                        }

                    } else {

                        if (type == Constants.UserMovieTab.HOPE_LOOK) {
                            _approveState.value = RequestResult(
                                error = R.string.request_fail
                            )
                        } else {
                            _haveSeenState.value = RequestResult(
                                error = R.string.request_fail
                            )
                        }

                    }
                }
            }
        }

        ThreadUtil.getInstance().execute {
            result = repository.chanUserAndMovieState(movieId, type)
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }
    }


}